package com.ll.daipao.config;

import com.ll.daipao.handler.UrlInterceptorHandler;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import redis.clients.jedis.JedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信公众号主配置
 * <p>
 *
 * @author FirenzesEagle
 * @author BinaryWang
 */
@Configuration
@ConfigurationProperties(prefix = "wxProperties", locations = "classpath:wx.properties")
public class MainConfiguration extends WebMvcConfigurerAdapter {
	private String appId;

	private String appSecret;

	private String token;

	private String aesKey;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getAppSecret() {
        return appSecret;
    }

    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }

    @Autowired
	private JedisPool jedisPool;

    @Bean
	public WxMpConfigStorage wxMpConfigStorage() {
		WxMpInMemoryConfigStorage configStorage = new WxMpInMemoryConfigStorage();
    //配置redis服务器
//    WxMpInRedisConfigStorage configStorage = new WxMpInRedisConfigStorage();
//    configStorage.setJedis(jedisPool.getResource());
//    configStorage.setAppId(this.appId);
//		configStorage.setSecret(this.appSecret);
//		configStorage.setToken(this.token);
//		configStorage.setAesKey(this.aesKey);
		return configStorage;
	}

	@Bean
	public WxMpService wxMpService() {
		WxMpService wxMpService = new WxMpServiceImpl();
		wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
		return wxMpService;
	}


    @Bean
    public WxMpMessageRouter wxMpMessageRouter() {
        WxMpMessageRouter wxMpMessageRouter = new WxMpMessageRouter( wxMpService());
        return wxMpMessageRouter;
    }

    @Bean
    public UrlInterceptorHandler urlInterceptorHandler() {
        return new UrlInterceptorHandler();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(urlInterceptorHandler()).addPathPatterns("/**")
                .excludePathPatterns("/core","/sigin","/getOAuth2UserInfo","/inviteFriend");
        super.addInterceptors(registry);
    }


}
